﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExportList.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Defines the ExportList type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.Wss.ContentMigration
{
    using System.Collections.Generic;

    using Collaboris.Saf.Engine;
    using Collaboris.SharePoint.Wss.ContentMigration;
    using Collaboris.SharePoint.Wss.Lists;
    using Collaboris.SharePoint.Wss.Webs;
    using Collaboris.Utils;

    using Microsoft.SharePoint;

    using SharePoint.Wss.Attributes;

    [CollaborisClass(
        Title = 
            "Exports a List to a CMP.", 
        ReleaseStatus = 
            ReleaseStatus.Stable, 
        DisableDocumentation = 
            false, 
        ContactAuthorEmail = 
            "Support@Collaboris.co.uk", 
        Summary =
            "Exports a SharePoint list to a Content Migration Package (CMP) " + 
            "based on the web and list name.",
        Remarks =
            "<p>The 'Do' method for this action uses the standard SharePoint content migration " +
            "API to achieve the export. " +
            "It is highly recommended that the server is running a minimum of SharePoint " + 
            "Service Pack 1.</p>" +
            @"This action can easily be paired with <a href=""C:\Temp\safDoc\Collaboris.Saf.Actions.Wss.ContentMigration.ImportList.html"">Import List</a>" +
            " to move content between sites and/or farms.",
          ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C633}")]
    public class ExportList : WSSActionBase<ListExportSettings>
    {
        #region Constants and Fields

        /// <summary>
        /// The helper.
        /// </summary>
        private ContentMigrationHelper helper = new ContentMigrationHelper();

        #endregion

        #region Helper Methods

        /// <summary>
        /// Executes the Action using the values passed in by the Data Entity
        /// and the <see cref="SafContext"/>.
        /// </summary>
        /// <param name="context">
        /// A valid <see cref="SafContext"/> containing properties to run the Action with.
        /// </param>
        /// <param name="entity">
        /// A Valid <see cref="T"/> containing the parameters with which to run the Action with.
        /// </param>
        /// <returns>
        /// A <see cref="ProcessStatus"/> signifying the outcome of processing the Action.
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, ListExportSettings entity)
        {
            this.Trace.TraceMethodStart("DoAction");

            ValidationHelper.VerifyObjectArgument(context.Web, "context.Web");
            
            SPList list = SPListHelper.GetList(TargetWeb, entity.ListName);
            ValidationHelper.VerifyObjectArgument(list, "list");

            if (list.ItemCount < 1)
            {
                this.Trace.TraceWarning(list.Title + "is empty so will not be exported.");
                return ProcessStatus.Warning;
            }
            
            this.helper.ExportList(entity.ExportSettings, list, TargetWeb.Url);
            
            this.Trace.TraceMethodEnd("DoAction");

            return ProcessStatus.Success;
        }

        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">
        /// The <see cref="SafContext"/> passed in by the SAF Engine.
        /// </param>
        /// <param name="entity">
        /// The data entity to Validate.
        /// </param>
        /// <param name="errors">
        /// List of <see cref="ValidationError"/>s
        /// </param>
        /// <param name="mode">
        /// <see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.
        /// </param>
        /// <returns>
        /// false if invalid; otherwise true.
        /// </returns>
        protected override bool IsValid(
            SafContext context, ListExportSettings entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            this.Trace.TraceMethodStart("IsValid");

            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.ListName, "ListExportSettings.ListName", ref errors);

            ValidateObjectForNull(entity.ExportSettings, "ListExportSettings.ExportSettings", ref errors);
            if (entity.ExportSettings != null)
            {
                ValidateStringForNull(
                    entity.ExportSettings.BaseFileName, "ListExportSettings.ExportSettings.BaseFileName", ref errors);
                ValidateStringForNull(
                    entity.ExportSettings.FileLocation, "ListExportSettings.ExportSettings.FileLocation", ref errors);
            }

            this.Trace.TraceMethodEnd("IsValid");

            // Have we added errors ?
            return errors.Count == currentCount;
        }

        #endregion
    }
}